<?php
namespace Addons\CustomQr\Services\Plugins;
use Models\BaseModel as Model;
use Addons\QyWechat\Helper;
use Addons\Fission\Helper as FissionHelper;
use Addons\QyWechat\Services\QyWechatApi;
use Addons\QyWechat\Services\ProvidersApi;

use Addons\WechatOpenPlatform\Services\Wxopenapi;
class Externalcontact
{
	public function add_external_contact(&$data,$arr=[]){
		$customqr = (new Model('qy_customqr'))->find(['state'=>$arr['State']]);
		if(!$customqr->id)return "不是该渠道";
		if($arr['WelcomeCode']){
			$welcomeCode = $arr['WelcomeCode'];
	    	$CorpID = $arr['ToUserName'];//企业微信CorpID 
			$qyConfig = (new Model('qy_wechat_config'))->find(['corp_id'=>$CorpID]);
			$QyWechatApi = QyWechatApi::getInstance();
			$etApp = $QyWechatApi->get_qy_app($qyConfig->corp_id,$qyConfig->externalcontact_secret);
			$url = FissionHelper::getInstance()->get_http_server_name();
			$welcome_list = json_decode($customqr->welcome_list,1);
  		    $qyUser = (new Model('qy_user_list'))->find(['wid'=>$qyConfig->wid,'userid'=>$arr['UserID']]);
			$qyCustomer = (new Model('qy_externalcontact_list'))->find(['wid'=>$qyConfig->wid,'user_id'=>$qyUser->id,'external_userid'=>$arr['ExternalUserID']]);
			if($customqr->auto_label == 1){
				$add_tag = json_decode($customqr->checkLable,1);
				$param = [
					'userid' => $arr['UserID'],
					'external_userid' => $arr['ExternalUserID'],
					'add_tag' => $add_tag,
				];
				if($add_tag){
					$editEtTagRes = $QyWechatApi->edit_et_tag($param,$qyConfig->wid);
					if($editEtTagRes["errcode"] == 0){
						$qyCustomer->tags_info = json_encode($add_tag);
						$qyCustomer->tags = json_encode($add_tag);
						$qyCustomer->save();
					}
				}
	 	 	}
			if($customqr->is_welcome == 1){
				$content = json_decode($customqr->welcome_text,1);
			  	$content = str_replace('<font style="color: green;">用户昵称</font>', $qyCustomer->name, $content);
				$content = str_replace('<div>', '
	', $content);
				$content = str_replace('&nbsp;', ' ', $content);
				$content = str_replace('<br>', '', $content);
				$content = str_replace('</div>', '', $content);
				$content = str_replace('amp;', '', $content);
				$msg = [
				   'text' => [
					  'content' => $content,
				   ]
			    ]; 
			}
			if(count($welcome_list) >= 1){
				foreach($welcome_list as $value){
					if($value["type"] == 2){
						if(strpos($value["image"],$_SERVER['HTTP_HOST'])){
						    $value["image"] = str_replace("https://".$_SERVER['HTTP_HOST'],".",$value["image"]);
						}
					 	$data[] = [
							'msgtype'=>'image',
							'image'=>[
								'media_id' => $etApp->media->uploadImage($value["image"])['media_id'],
							]
						];
					}else if($value["type"] == 3){
						$data[] = [
							'msgtype'=>'link',
							'link'=>[
								'title' => $value["title"],
								'picurl' => $value["picurl"],
								'desc' => $value["describe"],
								'url' => $value["url"]
							]
						];
					}else if($value["type"] == 4){
						if(strpos($value["image"],$_SERVER['HTTP_HOST'])){
						    $value["image"] = str_replace("https://".$_SERVER['HTTP_HOST'],".",$value["image"]);
						}
						$data[] = [
							'msgtype'=>'miniprogram',
							'miniprogram'=>[
								'title' => $value["title"],
								'pic_media_id' => $etApp->media->uploadImage($value["image"])['media_id'],
								'appid' => $value["appid"],
								'page' => $value["page"]
							]
						];
					}else if($value["type"] == 5){
						
					}
					$msg["attachments"] = $data;
				}
			}
			if($customqr->type == 1){
				if(strpos($customqr->pic_image,$_SERVER['HTTP_HOST'])){
				    $customqr->pic_image = str_replace("https://".$_SERVER['HTTP_HOST'],".",$customqr->pic_image);
				}
			 	$msg["attachments"][]  = [
					'msgtype'=>'miniprogram',
					'miniprogram'=>[
						'title' => $customqr->miniprogram_title,
						'pic_media_id' => $etApp->media->uploadImage($customqr->pic_image)['media_id'],
						'appid' => $customqr->miniprogram_appid,
						'page' => $customqr->miniprogram_page,
					]
				];
			}else{
				$msg["attachments"][]  = [
					'msgtype'=>'link',
					'link'=>[
						'title' => $customqr->text_title,
						'picurl' => $customqr->text_image,
						'desc' => $customqr->text_content,
						'url' => $customqr->text_url,
					]
				];
			}
			$res = $etApp->external_contact_message->sendWelcome($welcomeCode, $msg);
		}
	}
}
